import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ProgressMonitor;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;




public class GatewayFAs implements ActionListener
{
	DataOutputStream output;

	BufferedInputStream bis;
	
	BufferedOutputStream bos;
	String com;
	
	public String mip;
	public String dhmip;
	public String mhmip;

	byte[] receivedData;
	int in;
	
	String strLine;
	public Font f = new Font("Times new roman" , Font.BOLD , 18);	
	public Font f1 = new Font("Times new roman", Font.BOLD , 28);
	public Font f2 = new Font("Times new roman", Font.BOLD , 20);
	public Font f3 = new Font("Arial Rounded MT Bold", Font.BOLD , 20);
    public JLabel T1= new JLabel("GATEWAY FORIEGN AGENTS ");
	public JLabel T2= new JLabel();
	
	
	public JLabel l = new JLabel("Received File");
	public JLabel l1 = new JLabel("GATEWAY FORIEGN AGENT   A");
	public JLabel l2 = new JLabel("GATEWAY FORIEGN AGENT   B");
	public JLabel l3= new JLabel("Received File");
	
	
	
	public JButton btn = new JButton("Submit");
	
	public JTextArea tf = new JTextArea();
	public JScrollPane pane = new JScrollPane();
	
	public  JProgressBar pbar=new JProgressBar();
	public JProgressBar pbar1=new JProgressBar();
	public JProgressBar pbar2=new JProgressBar();
	
	public JButton graph = new JButton("Graphical");
	
	public JTextArea tf1 = new JTextArea();
	public JScrollPane pane1 = new JScrollPane();
	
	
	
	
	public JFrame jf;
	public Container c;
	JPanel jp=new JPanel();
	JPanel jp1=new JPanel();
	JPanel jp2=new JPanel();
	JPanel jp3=new JPanel();
	JPanel jp4=new JPanel();
	JPanel jp5=new JPanel();
	
	GatewayFAs()
	{
		
				
		jf = new JFrame(" Gateway Foriegn Agents");
		c = jf.getContentPane();
		c.setLayout(null);
		jf.setSize(1080,1080);
		
		c.setBackground(new Color(125,54,2));
		
		T1.setBounds(330,10,950,50);
		T2.setBounds(210,50,950,50);
		T1.setForeground(Color.GREEN);
		T2.setForeground(Color.RED);
		
		
		
		
		l.setBounds(190,148,250,35);
		
		l1.setBounds(120,110,350,35);
		l2.setBounds(610,110,350,35);
		l3.setBounds(690,148,250,35);
		
		
		
		l.setForeground(Color.BLUE);
		//l1.setForeground(new Color(120,0,0));
		l1.setForeground(Color.BLACK);
		l2.setForeground(Color.BLACK);
		l3.setForeground(Color.BLUE);
		
		
		l.setFont(f2);
		l1.setFont(f3);
		l2.setFont(f3);
		l3.setFont(f2);
		
		
		
		graph.setBounds(440,600,150,35);
		graph.setFont(f3);
	 	graph.setForeground(new Color(120,0,0));
			
		tf.setForeground(Color.BLUE);
		tf1.setForeground(new Color(69,39,4));
				
		tf.setFont(f);
		tf1.setFont(f);
	
		jp.setBounds(60,100,920,520);
		jp.setBackground(new Color(227,146,87));
		jp1.setBounds(55,95,930,530);
		
		
		jp2.setBounds(86,150,420,410);
		jp2.setBackground(new Color(227,146,87));
		jp3.setBounds(85,149,422,412);
		jp3.setBackground(Color.BLACK);
		
		jp4.setBounds(536,150,420,410);
		jp4.setBackground(new Color(227,146,87));
		jp5.setBounds(535,149,422,412);
		jp5.setBackground(Color.BLACK);
		
		pane.setBackground(new Color(151,242,241));
		tf.setColumns(20);
		tf.setRows(10);
		tf.setName("tf");
		pane.setName("pane");
		pane.setViewportView(tf);
		pane.setBounds(120,185,350,350);
		
        pane1.setBounds(570,185,350,350);
      
		tf1.setColumns(20);
		tf1.setRows(10);
		tf1.setName("tf");
		pane1.setName("pane");
		pane1.setViewportView(tf1);
				
		T1.setFont(f1);
		T2.setFont(f1);
			
        
		graph.addActionListener(this);		
		btn.setMnemonic(KeyEvent.VK_S); 
		jf.show();
		c.add(pane, BorderLayout.CENTER);
		c.add(pane1, BorderLayout.CENTER);
		c.add(T1);
		c.add(l1);
		c.add(l2);
		c.add(l);
		c.add(l3);
		c.add(jp2);
		c.add(jp3);
		c.add(jp4);
		c.add(jp5);
		c.add(jp);
		c.add(jp1);
		
		
			
		jf.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent win) {
				System.exit(0);
			}
		});		
		
		int[] ports = new int[] { 8889,9999 };

		for (int i = 0; i < 2; i++) {
			Thread t = new Thread(new PortListener(ports[i]));
			t.setName("Listener-" + ports[i]);
			t.start();

		}
		}
			
	
	
	

	public static void main(String args[])
	{
		GatewayFAs s=new GatewayFAs();
		
		
	}
	

	 public void actionPerformed(ActionEvent e)
	 {
		
	 }
	class PortListener implements Runnable {
		
		BufferedOutputStream bos = null;
		
		ServerSocket server;
		Socket connection;
		BufferedReader br = null;
		int port;

		public PortListener(int port) {
			this.port = port;
		}

		public void run() {
			if(this.port==8889)
			{
			try { 
				 
				
				    server = new ServerSocket(port);
					
				     while (true)
				   {
					                   connection = server.accept();
					
					                   br = new BufferedReader(
							           new InputStreamReader(new BufferedInputStream(
									   connection.getInputStream())));
					
					                      String strLine;
					                     // Read File Line By Line
					                   StringBuffer buffer = new StringBuffer();
					                   System.out.println("hi");
					                   while ((strLine = br.readLine()) != null)
					                  {
						                  // Print the content on the console
						                   System.out.println(strLine);
						                   buffer.append(strLine + "\n");

					                   }
				                               	br.close();
					                            connection.close();
					                            tf.setText(buffer.toString());	
					                            com=tf.getText();
					                            
					                            
									
					/******************************************************************/
					/*******************************************************************/
					                       byte[] byteArray;			 
					                       Socket client = null;
					    try     { 
						
						                  client = new Socket("127.0.0.1", 8888);
			    		                  bos = new BufferedOutputStream(client.getOutputStream()); 
			    		                  byteArray = tf.getText().getBytes();
			    		                  bos.write(byteArray, 0, byteArray.length);
			    		                  bos.flush();
			    		                  bos.close();
			    		                  client.close();
					           }
					              
					    catch (UnknownHostException e1) 
					          {
						        // TODO Auto-generated catch block
						           e1.printStackTrace();
					         }
					    catch (IOException e1) 
					         {
						       // TODO Auto-generated catch block
						          e1.printStackTrace();
					         }
					    
					    
					   
					    finally 
					           {
						            if (bos != null)
						            {
							                try 
							                  {
								              bos.close();
							                  }            
							                 catch (IOException e1)
							                   {
								                 // TODO Auto-generated catch block
								                 e1.printStackTrace();
							                    }
						           }
						 
						            try {
							                client.close();
					                   	} 
						            catch (IOException e1)
						                {
							             // TODO Auto-generated catch block
							               e1.printStackTrace();
						                }
					         }
							
	                  
				  
					
				        }
			  }       
			           catch (IOException e)
			            {
			        	   
			             }   
			           finally
			           {
			        	   
			            }
			
			
		           }
			else
				if(this.port==9999)
				{

					try { 
						 
						
						    server = new ServerSocket(port);
							
						     while (true)
						   {
							                   connection = server.accept();
							
							                   br = new BufferedReader(
									           new InputStreamReader(new BufferedInputStream(
											   connection.getInputStream())));
							
							                      String strLine;
							                     // Read File Line By Line
							                   StringBuffer buffer = new StringBuffer();
							                   System.out.println("hi");
							                   while ((strLine = br.readLine()) != null)
							                  {
								                  // Print the content on the console
								                   System.out.println(strLine);
								                   buffer.append(strLine + "\n");

							                   }
						                               	br.close();
							                            connection.close();
							                            tf1.setText(buffer.toString());	
											
							/******************************************************************/
							/*******************************************************************/
							                       byte[] byteArray;			 
							                       Socket client = null;
							    try     { 
								
								                  client = new Socket("127.0.0.1", 8888);
					    		                  bos = new BufferedOutputStream(client.getOutputStream()); 
					    		                  byteArray = tf1.getText().getBytes();
					    		                  bos.write(byteArray, 0, byteArray.length);
					    		                  bos.flush();
					    		                  bos.close();
					    		                  client.close();
							           }
							              
							    catch (UnknownHostException e1) 
							          {
								        // TODO Auto-generated catch block
								           e1.printStackTrace();
							         }
							    catch (IOException e1) 
							         {
								       // TODO Auto-generated catch block
								          e1.printStackTrace();
							         }
							    
							    
							   
							    finally 
							           {
								            if (bos != null)
								            {
									                try 
									                  {
										              bos.close();
									                  }            
									                 catch (IOException e1)
									                   {
										                 // TODO Auto-generated catch block
										                 e1.printStackTrace();
									                    }
								           }
								 
								            try {
									                client.close();
							                   	} 
								            catch (IOException e1)
								                {
									             // TODO Auto-generated catch block
									               e1.printStackTrace();
								                }
							         }
									
							
							
						        }
					  }       
					           catch (IOException e)
					            {
					        	   
					             }   
					           finally
					           {
					        	   
					            }
					
					
				           
				}
		}
		
		
		
		}
	 
}
